package com.peanut.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peanut.common.Result;
import com.peanut.domain.bo.TabNavigationBarBO;
import com.peanut.domain.po.TabNavigationBarPO;
import com.peanut.dto.TabNavigationBarDTO;
import com.peanut.service.TabNavigationBarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("navigationBar")
@Api("导航栏管理")
public class NavigationBarController {

    @Resource
    private TabNavigationBarService navigationBarService;

    /**
     * 添加导航栏数据
     * @param dto : 导航栏实体类中的数据
     * @return
     */
    @PostMapping
    @ApiOperation("添加导航栏数据")
    @ApiImplicitParam(name = "dto",value = "导航栏实体类",required = true,paramType ="body")
    public Result addNavigationBar(@RequestBody TabNavigationBarDTO dto){
        try {
            TabNavigationBarBO bo = new TabNavigationBarBO();
            BeanUtils.copyProperties(dto,bo);
            int i = navigationBarService.addNavigationBar(bo);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 查询导航栏
     * @return
     */
    @GetMapping("findAll")
    @ApiOperation("查询导航栏")
    public Result findAll(){
        try {
            List<TabNavigationBarPO> list = navigationBarService.findAll();
            return Result.OK().setData(list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 修改导航栏数据
     * @param dto : 导航栏实体类中的数据
     * @return
     */
    @PutMapping
    @ApiOperation("修改导航栏数据")
    @ApiImplicitParam(name = "dto",value = "导航栏实体类",required = true,paramType ="body")
    public Result updateNavigationBar(@RequestBody TabNavigationBarDTO dto){
        try {
            TabNavigationBarBO bo = new TabNavigationBarBO();
            BeanUtils.copyProperties(dto,bo);
            int i = navigationBarService.updateNavigationBar(bo);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 根据ID删除导航栏数据
     * @param navigationBarId : 导航栏ID
     * @return
     */
    @DeleteMapping("/{navigationBarId}")
    @ApiOperation("根据ID删除导航栏")
    @ApiImplicitParam(name = "navigationBarId",value = "导航栏ID",required = true,paramType ="path")
    public Result deleteNavigationBar(@PathVariable("navigationBarId") Long navigationBarId){
        try {
            int i = navigationBarService.deleteNavigationBar(navigationBarId);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }
}
